<template>
  <div class="log">
    <basic-container>
      <avue-crud
          ref="crud"
          :data="tableData"
          :option="tableOption"
          :page.sync="page"
          :permission="permissionList"
          :table-loading="tableLoading"
          @on-load="getList"
          @search-reset="searchReset"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @size-change="sizeChange"
          @current-change="currentChange"
          @row-update="handleUpdate"
          @selection-change="selectionChange">
        <template slot="state" slot-scope="scope">
          <el-tag v-if="scope.row.state === '0'" type="success">{{ scope.label }}</el-tag>
          <el-tag v-else-if="scope.row.state === '1'" type="danger">{{ scope.label }}</el-tag>
          <el-tag v-else-if="scope.row.state === null" type="info">未设置</el-tag>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
              class="filter-item"
              icon="el-icon-edit"
              size="mini"
              type="primary"
              @click="controlHandle(scope.row)">分配
          </el-button>
          <el-button
              class="filter-item"
              icon="el-icon-delete"
              size="mini"
              type="danger"
              @click="deleteHandle(scope.row)">清除
          </el-button>
        </template>
        <!--        <template slot="menuLeft">-->
        <!--          <el-button-->
        <!--              class="filter-item"-->
        <!--              icon="el-icon-plus"-->
        <!--              size="small"-->
        <!--              type="primary"-->
        <!--              @click="batchAddHandle()">批量分配时间方案-->
        <!--          </el-button>-->
        <!--        </template>-->
        <!--        <template slot="menuLeft">-->
        <!--          <el-button-->
        <!--              class="filter-item"-->
        <!--              icon="el-icon-delete"-->
        <!--              size="small"-->
        <!--              type="danger"-->
        <!--              @click="batchDelHandle()">批量清除时间方案-->
        <!--          </el-button>-->
        <!--        </template>-->
      </avue-crud>
      <!--      单个方案分配弹窗-->
      <el-dialog
          :visible.sync="dialogEditVisible"
          title="时间分配"
          center
          width="54%" @close="dictItemVisible">
        <el-form>
          <el-form-item>
            <span>当前学生：{{ currentrow.sysDeptName }}&nbsp;{{ currentrow.personName }}&nbsp;学号：{{
                currentrow.number
              }}</span>
          </el-form-item>
          <el-form-item>
            <h4>当前时间方案</h4>
            <el-button v-if="currentrow.schemeName === null" v-model="currentrow.schemeName" type="info">未设置</el-button>
            <el-button v-else v-model="currentrow.schemeName" type="primary">{{ currentrow.schemeName }}</el-button>
          </el-form-item>
          <el-form-item :span="24">
            <h4>选择时间方案(单选)</h4>
            <el-radio-group v-model="schid" @change="changeScheme">
              <el-radio v-for="(item,index) in schemeNames" :key="item.id" :label="item.id" :span="6"
                        style="text-align: left;margin: 4px 0 0 10px;width: 23.5%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
                        :value="item.schemeName" border size="medium">{{ item.schemeName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" :span="24">
          <el-button
              class="filter-item"
              size="small"
              type="primary"
              @click="handleUpdate()">保存分配
          </el-button>
        </div>
      </el-dialog>

      <!-- 清除单个方案弹窗-->
      <el-dialog
          :visible.sync="dialogDelVisible"
          center
          width="45%"
          title="清除时间方案" @close="dictDelVisible">
        <el-form>
          <el-form-item>
            <span>当前学生：{{ currentrow.sysDeptName }}&nbsp;{{ currentrow.personName }}&nbsp;学号：{{
                currentrow.number
              }}</span>
          </el-form-item>
          <el-form-item>
            <h4>当前时间方案</h4>
            <el-button v-if="currentrow.schemeName === null" v-model="currentrow.schemeName" type="info">未设置</el-button>
            <el-button v-else v-model="currentrow.schemeName" type="primary">{{ currentrow.schemeName }}</el-button>
          </el-form-item>
        </el-form>
        <div slot="footer" :span="24">
          <el-button
              class="filter-item"
              size="small"
              type="danger"
              @click="handleDel()">清除当前方案
          </el-button>
          <el-button
              class="filter-item"
              size="small"
              @click="dictDelVisible()">取消
          </el-button>
        </div>
      </el-dialog>

      <!--      批量分配时间方案弹窗-->
      <el-dialog
          :visible.sync="dialogbetchAddVisible"
          title="批量方案分配"
          width="54%"
          center @close="dictItemVisible">
        <el-form>
          <el-form-item>
            <span>当前筛选人数：{{ chooseList.length }}人</span>
          </el-form-item>
          <el-form-item :span="24">
            <h4>选择时间方案(单选)</h4>
            <el-radio-group v-model="schid" @change="changeScheme">
              <el-radio v-for="(item,index) in schemeNames" :key="item.id" :label="item.id" :span="6"
                        style="text-align: left;margin: 4px 0 0 10px;width: 23.5%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
                        :value="item.schemeName" border size="medium">{{ item.schemeName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" :span="24">
          <!--          <el-button-->
          <!--              class="filter-item"-->
          <!--              size="small"-->
          <!--              type="danger"-->
          <!--              @click="handleDel()">清除时间方案-->
          <!--          </el-button>-->
          <el-button
              class="filter-item"
              size="small"
              type="primary"
              @click="batchSaveHandle()">保存分配
          </el-button>
        </div>
      </el-dialog>

    </basic-container>
  </div>
</template>
<script>
import {mapGetters} from "vuex";
import {addBatchObj, addScheduleObj, delIdsObj, delScheduleObj, timegroupObj} from "@/api/pass/s-timegroup";
import {passIndexObj} from "@/api/pass/pass";
import {GROUP_SEX, GROUP_STYLE, PASS_DETAILS} from '@/const/Dict'
import {PERSON_OPERA} from "@/const/personDict";

export default {
  name: "s-timegroup",
  data() {
    return {
      dialogbetchAddVisible: false,
      dialogbetchDelVisible: false,
      dialogEditVisible: false,
      dialogDelVisible: false,
      tableData: [],
      searchForm: {},
      area: '',
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      // tableOption: tableOption,
      //单个分配
      schemeNames: [],
      currentrow: {},
      schid: '',
      chooseList: [],
      tableOption: {
        border: true,
        index: true,
        indexLabel: '序号',
        stripe: true,
        menuAlign: 'center',
        menuWidth: 200,
        menuType: 'button',
        align: 'center',
        emptyBtnText: '重置', //清空按钮文案
        refreshBtn: true,
        showClomnuBtn: false,
        searchSize: 'mini',
        searchMenuSpan: 4,
        searchLabelWidth: 80,
        searchSpan: 4,
        labelWidth: "20%",
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        selection: true,
        reserveSelection: true,
        props: {
          label: 'label',
          value: 'value'
        },
        column: [
          {
            label: '人员id',
            prop: 'id',
            hide: true
          },
          {
            label: '姓名',
            prop: 'personName',
            type: 'input',
            search: true,
          }, {
            label: '学号',
            prop: 'number',
            dicData: PASS_DETAILS,
          }, {
            label: '性别',
            prop: 'sex',
            type: 'select',
            search: true,
            dicData: GROUP_SEX,
            change: (value) => {
              this.searchForm.sex = value.value
              this.getList(this.page, this.searchForm)
            },
          }, {
            label: '年级/班级',
            prop: 'sysDeptId',
            search: true,
            filter: false,
            type: 'tree',
            hide: true,
            dicUrl: '/campus/sysdept/detpTree/1/2,3',
            dicMethod: 'get',
            props: {
              label: 'name',
              value: 'deptId'
            },
            change: (value) => {
              this.searchForm.sysDeptId = value.value
              this.getList(this.page, this.searchForm)
            },
          },
          {
            label: '年级',
            prop: 'deptParentName',
            dicData: PASS_DETAILS,
          }, {
            label: '班级',
            prop: 'sysDeptName',
            dicData: PASS_DETAILS,
          }, {
            label: '学生属性',
            prop: 'identityKey',
            search: true,
            type: 'select',
            dicUrl: '/campus/identity/selectAll?groupNum=2',
            dicMethod: 'get',
            props: {
              label: 'name',
              value: 'id'
            },
            change: (value) => {
              this.searchForm.identityKey = value.value
              this.getList(this.page, this.searchForm)
            },
          }, {
            label: '楼栋',
            prop: 'buildingName',
            dicData: PASS_DETAILS,
          }, {
            label: '楼栋',
            prop: 'buildingId',
            hide: true,
            search: true,
            type: 'select',
            dicUrl: '/campus/building/selectAll',
            dicMethod: 'get',
            // searchClearable: false,
            props: {
              label: 'buildingName',
              value: 'buildingId'
            },
            change: (value) => {
              this.searchForm.buildingId = value.value
              this.getList(this.page, this.searchForm)
            },
          }, {
            label: '宿舍号',
            prop: 'dormNumber',
            dicData: PASS_DETAILS,
            search: true,
            type: 'input',
          }, {
            width: 150,
            label: '时间方案',
            prop: 'schemeName',
            dicData: PASS_DETAILS,
          }, {
            width: 150,
            label: '人员修改时间',
            prop: 'updateTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            addDisplay: false,
            editDisplay: false,
          }, {
            width: 100,
            label: '修改操作人',
            prop: 'operator',
            dicData: PERSON_OPERA,
          }, {
            label: '状态',
            prop: 'state',
            type: 'select',
            search: true,
            slot: true,
            dicData: GROUP_STYLE,
            change: (value) => {
              this.searchForm.state = value.value
              this.getList(this.page, this.searchForm)
            },
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permissions.sys_time_del, false),
      }
    }
  },
  watch: {
    selectionChange: (val) => {
      if (this.chooseList.length === 0) {
        this.$refs.crud.toggleSelection(val);
      }
    },
    immediate: true
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true
      timegroupObj(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      })
    },
    //清楚单个方案分配弹窗
    deleteHandle(row) {
      this.dialogDelVisible = true
      this.currentrow = row
    },
    //学生时间组分配查询
    // 查询所有的分配方案展示供分配
    controlHandle(row) {
      this.dialogEditVisible = true
      this.currentrow = row
      // console.log(this.currentrow)
      let param = {
        "size": "999"
      }
      passIndexObj(param).then(res => {
        if (res.data.code === 0) {
          this.schemeNames = res.data.data.records
        }
      })
    },
    // 选择要分配的方案
    changeScheme(value) {
      this.schid = value
      console.log(value)
    },
    // 保存单个时间方案分配
    handleUpdate: function (row, index, done) {
      // debugger
      if (this.schid === "") {
        this.$message.error('请选择要分配的时间方案')
      } else {
        let param = {
          personId: this.currentrow.id,
          schemeId: this.schid
        }
        // console.log(param)
        addScheduleObj(param).then(res => {
          if (res.data.code === 0) {
            this.$message.success('分配成功')
          } else {
            this.$message.error('分配失败')
          }
          // done()
          this.dialogEditVisible = false
          this.getList(this.page)
        })
      }
    },
    //关闭单个分配弹窗
    dictItemVisible: function () {
      this.dialogEditVisible = false
      this.schid = null
    },
    //关闭单个清除方案弹窗
    dictDelVisible: function () {
      this.dialogDelVisible = false
    },
    // 清除单个时间方案
    handleDel: function (row, index) {
      delScheduleObj(this.currentrow.id).then(res => {
        if (res.data.code === 0) {
          this.$message.success('清除成功')
        } else {
          this.$message.error('清除失败')
        }
        this.dialogEditVisible = false
        this.dialogDelVisible = false
        this.getList(this.page)
      })
    },
    // 表格的多选方法
    selectionChange(list) {
      // this.$message.success('选中的数据'+ JSON.stringify(list));
      this.chooseList = list
      // console.log(this.chooseList)
    },
    //批量分配时间方案
    //获取所有可选方案
    batchAddHandle: function () {
      this.dialogbetchAddVisible = true
      let param = {
        "size": "999"
      }
      passIndexObj(param).then(res => {
        if (res.data.code === 0) {
          this.schemeNames = res.data.data.records
        }
      })
    },
    //进行批量分配操作
    batchSaveHandle: function (val) {
      // debugger

      this.chooseList.forEach((value, index) => {
        value['schemeId'] = this.schid
      })
      // console.log(this.chooseList)

      var arr = [];
      this.chooseList.forEach(function (item) {
        arr.push({schemeId: item.schemeId, personId: item.id})
      })
      console.log(arr)
      addBatchObj(arr).then(res => {
        if (res.data.code === 0) {
          this.$message.success('分配成功')
        } else {
          this.$message.error('分配失败')
        }
        this.dialogbetchAddVisible = false
        this.chooseList.length = 0
        console.log(this.chooseList)
        // this.val ==null
        this.getList(this.page)
        this.$refs.crud.toggleSelection(val);
      })
    },

    //批量清除时间方案
    batchDelHandle: function (val) {
      let ids = this.chooseList.map(x => {
        return x.id
      })

      if (ids.length === 0) {
        this.$message.error('请选择要清除方案的人员')
      } else {
        delIdsObj(ids).then(res => {
          if (res.data.code === 0) {
            this.$message.success('清除成功')
          } else {
            this.$message.error('清除失败')
          }
          this.dialogbetchDelVisible = false
          this.getList(this.page)
          this.$refs.crud.toggleSelection(val);
        })
      }
    },
    // handleDel: function (row) {
    //   debugger
    //   this.$confirm('清除当前"' + row.schemeName + '"的时间方案?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function () {
    //     return delScheduleObj(row.personId)
    //   }).then(() => {
    //     this.getList(this.page)
    //     this.$message.success('删除成功')
    //   })
    // },
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    refreshChange() {
      this.getList(this.page)
    },
    searchReset() {
      this.searchForm = {};
      this.getList(this.page)
    }
  }
}
</script>

<style scoped>
h4 {
  margin: 0;
}
</style>